<?php

/**
 * 文档栏目 - 数据模型
 * 
 * @package    01CMS
 * @subpackage admin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class CategoryModel
{
    public $id;
    public $data;
    
    /**
     * 栏目递归获取父类目时，栏目数据累加到这个属性
     *
     * @var int
     */
    public $parentCategory = array();
    
    /**
     * 栏目递归获取子类目时，栏目ID累加到这个属性
     *
     * @var int
     */
    public $categoryIds = '';
    
    private $Db;
    
    /**
     * 栏目递归获取子类目时，栏目数据累加到这个属性
     *
     * @var int
     */
    private $listArray = array();

    function __construct ($id = 0)
    {
        if (intval($id) > 0)
        {
            $category = $this->getData();
            
            $this->id = $id;
            $this->data = isset($category[$id]) ? $category[$id] : array();
        }
        
        $this->Db = model('Db');
    }

    function getData ()
    {
        if (data('archiveCategory') === FALSE)
        {
            $this->writeChannel();
        }
        
        return data('archiveCategory');
    }

    /**
     * 获取父类
     *
     * @param int $id
     * @param int $all [1|0]
     * @param int $onlyInner [0|1]
     */
    function getParent ($id, $sign = 0)
    {
        if ($sign == 0)
        {
            $this->parentCategory = array();
            $sign = 1;
        }
        
        $id = intval($id);
        
        $this->Db->query("SELECT * FROM @@__archive_category WHERE id={$id} AND type != 0", $id);
        
        while ($row = $this->Db->getNextRow($id))
        {
            $this->parentCategory[] = $row;
            
            $this->getParent($row->parentId, $sign);
        }
    }

    /**
     * 获取类目子ID
     *
     * @param int $parentId [0]
     * @param int $all [1|0]
     * @param int $onlyInner [0|1]
     */
    function getChildIds ($parentId = 0, $all = 1, $onlyInner = 0, $sign = 0)
    {
        if ($sign == 0)
        {
            $this->parentCategory = array();
            $sign = 1;
        }
        
        $parentId = intval($parentId);
        
        $type = $onlyInner ? "AND type != 0" : '';
        
        $this->Db->query("SELECT id FROM @@__archive_category WHERE isHidden = 0 AND parentId={$parentId} {$type}", $parentId);
        
        while ($row = $this->Db->getNextRow($parentId))
        {
            if ($this->categoryIds == '')
            {
                $this->categoryIds = $row->id;
            }
            else
            {
                $this->categoryIds .= ',' . $row->id;
            }
            
            if ($all)
            {
                $this->getChildIds($row->id, $all, $sign);
            }
        }
    }

    /**
     * 获取子类目
     *
     * @param int $parentId 父ID[0]
     * @param int $recursion 是否递归[0|1]
     * @param int $onlyInner 是否递归[0|1]
     * @param int $logicSign 是否添加逻辑标记[1|0]
     * @param string $separation [└─]
     * @param string $addSeparation [─]
     * @return object
     */
    function getChildren ($parentId = 0, $recursion = 0, $onlyInner = 0, $logicSign = 1, $separation = '└─', $addSeparation = '─', $where = '1')
    {
        $parentId = intval($parentId);
        
        $this->listArray = array();
        
        $type = $onlyInner ? "AND type != 0" : '';
        
        $this->Db->query("SELECT * FROM @@__archive_category WHERE {$where} AND parentId ={$parentId} {$type} ORDER BY ordering ASC", 'children');
        
        while ($row = $this->Db->getNextRow('children'))
        {
            $this->listArray[] = $row;
            
            if ($recursion)
            {
                $this->getLogicCategory($row->id, $type, $logicSign, $separation, $addSeparation, $where);
            }
        }
        
        return $this->listArray;
    }

    /**
     * 仅供getChildren()调用
     */
    function getLogicCategory ($id, $type, $logicSign, $separation, $addSeparation, $where)
    {
        $this->Db->query("SELECT * FROM @@__archive_category where $where AND parentId={$id} {$type} ORDER BY ordering ASC", 'logic' . $id);
        
        while ($row = $this->Db->getNextRow('logic' . $id))
        {
            if ($logicSign)
            {
                $row->name = $separation . $row->name;
            }
            
            $this->listArray[] = $row;
            
            $this->getLogicCategory($row->id, $type, $logicSign, $separation . $addSeparation, $addSeparation);
        }
    }

    function delete ()
    {
        if ($this->allSonIds != '')
        {
            $this->Db->delete("categoryId in ({$this->id},{$this->allSonIds})", '@@__archive_archive');
            $this->Db->delete("id in ({$this->id},{$this->allSonIds})", '@@__archive_category');
        }
        else
        {
            $this->Db->delete("categoryId in ({$this->id})", '@@__archive_archive');
            $this->Db->delete("id in ({$this->id})", '@@__archive_category');
        }
        
        $this->writeCategory();
    }

    function writeCategory ()
    {
        $category = array();
        
        $ids = array();
        
        $this->Db->query('select * from `@@__archive_category`', 'category');
        
        while ($row = $this->Db->getNextRow('category'))
        {
            $alias = empty($row->alias) ? $row->id : preg_replace('/[^a-zA-Z0-9-_]/i', '', $row->alias);
            
            $ids[$alias] = $row->id;
            
            $category[$row->id] = array('name'=>$row->name, 'isHidden'=>$row->isHidden, 'type'=>$row->type, 'alias'=>$alias, 'channelId'=>$row->channelId, 'pageSize'=>$row->pageSize, 'categoryTemplateName'=>$row->categoryTemplateName, 'archiveTemplateName'=>$row->archiveTemplateName, 'archiveUrl'=>$row->archiveUrl, 'link'=>$row->link);
            
            $this->categoryIds = '';
            $this->getChildIds($row->id, FALSE);
            $category[$row->id]['sonIds'] = $this->categoryIds;
            
            $this->categoryIds = '';
            $this->getChildIds($row->id, true);
            $category[$row->id]['allSonIds'] = $this->categoryIds;
        }
        
        $category['ids'] = $ids;
        
        lib('Write')->phpArray(SYS_PATH . '/data/archiveCategory.php', $category);
        
        model('Channel')->writeChannel();
    }

    public function __get ($key)
    {
        if (isset($this->$key))
        {
            return $this->$key;
        }
        else if (isset($this->data[$key]))
        {
            return $this->data[$key];
        }
        else
        {
            return '';
        }
    }
}